package cn.com.surker.sale.controller;

import cn.com.surker.base.BaseEntity;
import cn.com.surker.base.pump.entity.PumpNorm;
import cn.com.surker.base.pump.entity.PumpType;
import cn.com.surker.base.pump.service.PumpNormService;
import cn.com.surker.base.pump.service.PumpTypeService;
import cn.com.surker.base.strength.dao.BaseStrengthSpecialDao;
import cn.com.surker.base.strength.entity.BaseStrength;
import cn.com.surker.base.strength.entity.BaseStrengthSpecial;
import cn.com.surker.base.strength.service.StrengthBaseService;
import cn.com.surker.base.strength.service.StrengthSpecialBaseService;
import cn.com.surker.common.AbstractController;
import cn.com.surker.common.Code;
import cn.com.surker.enums.sale.ContractQualityEnum;
import cn.com.surker.production.entity.PumpDevice;
import cn.com.surker.production.service.PumpDeviceService;
import cn.com.surker.sale.entity.SaleContract;
import cn.com.surker.sale.service.SaleContractBaseService;
import cn.com.surker.sale.service.impl.SaleContractServiceImpl;
import cn.com.surker.spring.RestResultDto;
import cn.com.surker.vo.sale.SaleStrengthSpecialPriceVo;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 销售合同表 前端控制器
 * </p>
 *
 * @author admin
 * @since 2024-06-18
 */

@RestController
@RequestMapping("/sale/contract")
public class SaleContractController extends AbstractController {

	//	销售合同类
	@Autowired
	private SaleContractServiceImpl contractService;

	@Resource
	private StrengthBaseService strengthService;
	//	泵送设备
	@Resource
	private PumpDeviceService pumpDeviceService;
	@Resource
	protected PumpNormService pumpNormService;
	@Resource
	protected PumpTypeService pumpTypeService;

	@Autowired
	private StrengthSpecialBaseService strengthSpecialService;


	/**
	 * 查询合同列表
	 *
	 * @return
	 */
	@RequestMapping(value = "/list", method = RequestMethod.POST)
	@ResponseBody
	public RestResultDto conList(String searchVal, Integer workStatus) {
		// List<SaleContract> contractList = contractService.queryConList(null, orgId, null, searchVal, null, null, workStatus, null, null, null, null, null);
		List<SaleContract> contractList = contractService.queryVagueList(orgId, searchVal, null, workStatus, null, null);
		return RestResultDto.success(contractList);
	}

	/**
	 * 查询合同列表
	 *
	 * @return
	 */
	@RequestMapping(value = "/page", method = RequestMethod.POST)
	@ResponseBody
	public RestResultDto conPage(String conName, Integer workStatus) {
		IPage page = contractService.findPage(conName, orgId, workStatus, null, null, workStatus, null, null, null,
			null, null, null, null, 1, 15);
		List records = page.getRecords();
		return RestResultDto.success(records);
	}


	/**
	 * 合同下的强度等级
	 * @param conId
	 * @return
	 */
	@RequestMapping(value = "/strength/list", method = RequestMethod.POST)
	@ResponseBody
	public RestResultDto strengthList(Long conId) {
		if (conId == null) {
			return RestResultDto.fail(Code.PARAM_EMPTY);
		}
		//	查询合同数据
		SaleContract contract = contractService.getById(conId);
		if (!BaseEntity.validateExist(contract)) {
			return RestResultDto.fail(Code.NO_PRODUCTION_CONTRACT_EMPTY);
		}
		//	判断合同类型,零星合同选择全部,
		List<BaseStrength> strengthList;
		Integer quality = contract.getQuality();
		/*if (ContractQualityEnum.LXHT.getCode().equals(quality)) {
			strengthList = strengthService.findAll();
		} else {
			strengthList = strengthService.findBySaleCon(conId);
		}*/
		strengthList = strengthService.findBySaleCon(conId);
		return RestResultDto.success(strengthList);
	}

	@RequestMapping(value = "/tech/list", method = RequestMethod.POST)
	@ResponseBody
	public RestResultDto techList(Long conId) {
		//	查询合同数据
		SaleContract contract = contractService.getById(conId);
		if (!BaseEntity.validateExist(contract)) {
			return RestResultDto.fail(Code.NO_PRODUCTION_CONTRACT_EMPTY);
		}
		//	判断合同类型,零星合同选择全部,
		List<SaleStrengthSpecialPriceVo> strengthSpecialList = strengthSpecialService.findByConId(erpDb, orgId, conId);
		return RestResultDto.success(strengthSpecialList);
	}

	/**
	 * 根据销售合同查询绑定的泵送设备
	 *
	 * @param conId
	 * @return
	 */
	@RequestMapping(value = "/pump/device/list", method = RequestMethod.POST)
	@ResponseBody
	public RestResultDto pumpDeviceList(Long conId) {
		if (conId == null) {
			return RestResultDto.fail(Code.PARAM_EMPTY);
		}
		//	查询合同数据
		SaleContract contract = contractService.getById(conId);
		if (!BaseEntity.validateExist(contract)) {
			return RestResultDto.fail(Code.NO_PRODUCTION_CONTRACT_EMPTY);
		}
		List<PumpDevice> pumpDeviceList;
		Integer quality = contract.getQuality();
		if (!PUMP_ENABLE_QUALITY || ContractQualityEnum.LXHT.getCode().equals(quality)) {
			//	零星查全部
			pumpDeviceList = pumpDeviceService.findOrgDevice(orgId);
		} else {
			pumpDeviceList = pumpDeviceService.findBySaleCon(orgId, conId);
		}
		return RestResultDto.success(pumpDeviceList);
	}

	@RequestMapping(value = "/pump/type/list", method = RequestMethod.POST)
	@ResponseBody
	public RestResultDto pumpTypeList(Long conId) {
		if (conId == null) {
			return RestResultDto.fail(Code.PARAM_EMPTY);
		}
		//	查询合同数据
		SaleContract contract = contractService.getById(conId);
		if (!BaseEntity.validateExist(contract)) {
			return RestResultDto.fail(Code.NO_PRODUCTION_CONTRACT_EMPTY);
		}
		List<PumpType> pumpTypeList;
		Integer quality = contract.getQuality();
		if (!PUMP_ENABLE_QUALITY || ContractQualityEnum.LXHT.getCode().equals(quality)) {
			//	零星查全部
			pumpTypeList = pumpTypeService.findList(null);
		} else {
			pumpTypeList = pumpTypeService.findBySaleCon(conId);
		}
		return RestResultDto.success(pumpTypeList);
	}

	@RequestMapping(value = "/pump/norm/list", method = RequestMethod.POST)
	@ResponseBody
	public RestResultDto pumpNormList(Long conId) {
		if (conId == null) {
			return RestResultDto.fail(Code.PARAM_EMPTY);
		}
		//	查询合同数据
		SaleContract contract = contractService.getById(conId);
		if (!BaseEntity.validateExist(contract)) {
			return RestResultDto.fail(Code.NO_PRODUCTION_CONTRACT_EMPTY);
		}
		List<PumpNorm> pumpNormList;
		Integer quality = contract.getQuality();
		if (!PUMP_ENABLE_QUALITY || ContractQualityEnum.LXHT.getCode().equals(quality)) {
			//	零星查全部
			pumpNormList = pumpNormService.findList(null);
		} else {
			pumpNormList = pumpNormService.findBySaleCon(conId);
		}
		return RestResultDto.success(pumpNormList);
	}
}



